package com.inxedu.os.edu.controller.main;

import com.inxedu.os.common.util.WebUtils;
import com.inxedu.os.edu.pojo.SysLoginLog;
import com.inxedu.os.edu.pojo.SysUser;
import com.inxedu.os.edu.service.main.SysLoginLogService;
import com.inxedu.os.edu.service.main.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/admin")
@Api(tags = "登录")
public class LoginController {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysLoginLogService sysLoginLogService;

 //   @ApiOperation(value = "进入登陆页面")
    @RequestMapping(method = {RequestMethod.GET,RequestMethod.POST})
//    @ResponseBody
    public String toLogin(){


        return "view/inxedu/admin/main/login";
    }
    @ApiOperation(value = "登录时的操作")
    @RequestMapping(value = "/main/login",method = RequestMethod.POST)
    public String toMain(HttpServletRequest request,Model model){
//        String validateCode = (String) request.getSession().getAttribute("validateCode");
//        if (request.getParameter("randomCode")==null||request.getParameter("randomCode").trim().equals("")){
//            model.addAttribute("message","验证码不能为空！");
//            return "view/inxedu/admin/main/main";
//        }
//        if (!validateCode.equals(request.getParameter("randomCode"))){
//            model.addAttribute("message","验证码错误!");
//            return "view/inxedu/admin/main/main";
//        }
        request.getSession().removeAttribute("validateCode");
        SysUser su = sysUserService.queryLoginUser(request.getParameter("sysUser.loginName"));
        System.out.println(su);
//        model.addAttribute("loginName",su.getLoginName());
        request.getSession().setAttribute("loginName",su.getLoginName()+"");
//       redisTemplate.boundValueOps("user").set(JsonUtils.objectToJson(su));
         request.getSession().setAttribute("userId",su.getUserId());
//            String header = request.getHeader("x-forwarded-for");
//            System.out.println(header);
//        System.out.println("-------------------------------------------------------"+WebUtils.getIP(request));
        sysUserService.updateUserLoginLog(su.getUserId(), new Date(),WebUtils.getIP(request));
        String username=SecurityContextHolder.getContext().getAuthentication().getName();
        SysLoginLog loginLog=new SysLoginLog();
        loginLog.setUserId(su.getUserId());
        loginLog.setLoginTime(new Date());
        loginLog.setIp(WebUtils.getIP(request));
        String userAgent = WebUtils.getUserAgent(request);
        if(!userAgent.isEmpty()){
            loginLog.setUserAgent(userAgent.split(";")[0]);//浏览器
            loginLog.setOsName(userAgent.split(";")[1]);//操作系统
        }
        sysLoginLogService.createLoginLog(loginLog);
        return "redirect:/admin/main";
    }


    @ApiOperation(value = "退出系统")
    @RequestMapping(value = "/outlogin",method = RequestMethod.GET)
    public String exit(HttpServletRequest request){
        request.getSession().invalidate();
//        stringRedisTemplate.delete();

        return "redirect:/admin";
    }
    @ApiOperation(value = "账号密码错误返回登陆页面")
    @RequestMapping(value = "/error",method = {RequestMethod.GET,RequestMethod.POST})
    public String error(Model model){
        model.addAttribute("message","用户名或密码错误！");
        return "view/inxedu/admin/main/login";
    }
}
